package tr.com.netgis;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import tr.com.netgis.util.EulaUtils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;

/**
 * 
 * @author NetGis
 *
 */
public class ServiceAgreementActivity extends Activity implements OnClickListener {

	private WebView webView;
	private Button btnReject;
	private Button btnAccept;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.service_agreement);
		
		if (EulaUtils.getEulaValue(this)) {
			finish();
			startActivity(new Intent(this, SplashScreen.class));
			return;
		}
		
		this.btnAccept = (Button) findViewById(R.id.serviceAgreementBtnAccept);
		this.btnReject = (Button) findViewById(R.id.serviceAgreementBtnReject);
		this.webView = (WebView) findViewById(R.id.serviceAgreementWebView);
		this.webView.loadData(readAgreement(this), "text/html", null);
		this.btnAccept.setOnClickListener(this);
		this.btnReject.setOnClickListener(this);
	}
	
	public static String readAgreement (Context ctx) {
		String str="";
		try {
			StringBuffer buf = new StringBuffer();			
			InputStream is = ctx.getResources().openRawResource(R.drawable.service_agreement);
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			if (is!=null) {							
				while ((str = reader.readLine()) != null) {	
					buf.append(str + "\n" );
				}				
			}		
			is.close();				
			return buf.toString();
		} catch (Exception e) {
			return e.getMessage();
		}
	}

	@Override
	public void onClick(View v) {
		finish();
		if (v == btnAccept) {
            EulaUtils.setEulaValue(this);
			startActivity(new Intent(this, SplashScreen.class));
		} else if (v == btnReject) {
		}
	}
}
